package leetcode.力扣竞赛.第98场双周赛;

import java.util.LinkedList;
import java.util.List;

/**
 * @author: yan
 * @description: 替换一个数字后的最大差值
 * @create: 2023-02-20 15:56
 **/
public class Solution1566 {

    public static void main(String[] args) {
        System.out.println(minMaxDifference(11891));
    }

    public static int minMaxDifference(int num) {
        int maxLoc = 9, minLoc = 0, max = 0, min = 0;
        List<Integer> list = new LinkedList<>();
        while (num > 0) {
            list.add(num%10);
            num /= 10;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(list.size() - i - 1) != 9) {
                maxLoc = list.get(list.size() - i - 1);
                break;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(list.size() - i - 1) != 0) {
                minLoc = list.get(list.size() - i - 1);
                break;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            int curNum = list.get(list.size() - i - 1);

            if (curNum == maxLoc) {
                max = max * 10 + 9;
            } else {
                max = max * 10 + curNum;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            int curNum = list.get(list.size() - i - 1);

            if (curNum == minLoc) {
                min = min * 10 + 0;
            } else {
                min = min * 10 + curNum;
            }
        }
        return max - min;
    }

}
